<template>
  <page-header-wrapper>
    <a-spin :spinning="searchLoading">
      <GlobalSearch :list="searchList" @search="handleSearch" @reset="handleReset"> </GlobalSearch>
    </a-spin>
    <a-card :bordered="false">
      <s-table
        ref="table"
        size="default"
        rowKey="ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: (total) => `共 ${total} 条` }"
        :scroll="{ x: true }"
      >
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }} </span>
        <div slot="action" slot-scope="text, record">
          <y-button type="link" @click="handleDetail(record)">查看</y-button>
          <y-button v-if="record.PactClass !== '终止合同'" type="link" class="text-warning" @click="handleEdit(record)"
            >修改合同</y-button
          >
        </div>
      </s-table>
    </a-card>
    <sign-contract
      :visible.sync="visibleSign"
      :data.sync="contract"
      @success="tableRefresh(true)"
      title="修改合同"
      pactClass="新签合同"
      type="continue"
      :contractID="contract.ID"
    ></sign-contract>
    <detail :visible.sync="visibleDetail" :data.sync="contract"></detail>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis, signContract } from '@/components'
import { getContractAccount } from '@/api/followUpManagement'
import { getDropdownList } from '@/api/common'
import detail from '@/components/siginContract/modalContract.vue'
import moment from 'moment'
export default {
  components: {
    STable,
    Ellipsis,
    signContract,
    detail,
  },
  data() {
    return {
      searchLoading: false,
      contract: {},
      visibleDetail: false,
      visibleSign: false,
      queryParam: {
        PactClass: '',
        strContent: {},
        DeptNO: this.$store.getters.userInfo.DeptNO,
      },
      searchList: [
        {
          label: '合同类别',
          fields: 'PactClass',
          type: 'select',
          props: {
            placeholder: '请选择合同类别',
            options: [],
          },
        },
        {
          label: '合同状态',
          fields: 'Status',
          type: 'select',
          props: {
            placeholder: '请选择合同状态',
            options: [],
          },
        },
        {
          label: '合同编号',
          fields: 'PactNo',
          type: 'input',
          props: {
            placeholder: '请输入合同编号',
          },
        },
        // {
        //   label: '合同名称',
        //   fields: 'UnitName',
        //   type: 'input',
        //   props: {
        //     placeholder: '请输入合同名称',
        //   },
        // },
        {
          label: '甲方(单位)',
          fields: 'UnitName',
          type: 'input',
          props: {
            placeholder: '请输入甲方单位信息',
          },
        },
        {
          label: '申请编号',
          fields: 'ApplyNo',
          type: 'input',
          props: {
            placeholder: '请输入申请编号',
          },
        },
        {
          label: '乙方姓名',
          fields: 'Name',
          type: 'input',
          props: {
            placeholder: '请输入乙方姓名',
          },
        },
        {
          label: '乙方身份证号',
          fields: 'IDCard',
          type: 'input',
          props: {
            placeholder: '请输入乙方身份证号',
          },
        },
        {
          label: '房屋地址',
          fields: 'EAddress',
          type: 'input',
          props: {
            placeholder: '请输入房屋地址',
          },
        },
        {
          label: '租赁时间',
          fields: 'date',
          type: 'range',
          props: {
            valueFormat: 'YYYY-MM-DD',
          },
        },
        {
          label: '签订时间',
          fields: 'SignDate',
          type: 'date',
          props: {
            placeholder: '请选择签订时间',
            valueFormat: 'YYYY-MM-DD',
          },
        },
      ],
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'index' },
        },
        {
          title: '合同编号',
          align: 'center',
          dataIndex: 'PactNo',
        },
        {
          title: '申请编号',
          align: 'center',
          dataIndex: 'ApplyNo',
        },
        {
          title: '甲方(单位)',
          align: 'center',
          dataIndex: 'UnitName',
        },
        {
          title: '乙方租户',
          align: 'center',
          dataIndex: 'Name',
        },
        {
          title: '房屋信息',
          align: 'center',
          dataIndex: 'EAddress',
          customRender: (text) => <ellipsis length={28}>{text || '/'}</ellipsis>,
        },
        {
          title: '合同类型',
          align: 'center',
          dataIndex: 'PactClass',
        },
        {
          title: '合同状态',
          align: 'center',
          dataIndex: 'Status',
          customRender: (text) => {
            const textColor = new Map([
              ['已签', 'text-success'],
              ['待签', 'text-warning'],
              ['续签', 'text-primary'],
              ['过期', 'text-error'],
            ])
            return <span class={textColor.get(text)}>{text}</span>
          },
        },
        {
          title: '起租日期',
          align: 'center',
          dataIndex: 'StartDate',
          customRender: (text) => moment(text).format('YYYY-MM-DD'),
        },
        {
          title: '止租日期',
          align: 'center',
          dataIndex: 'EndDate',
          customRender: (text) => moment(text).format('YYYY-MM-DD'),
        },
        {
          title: '签订日期',
          align: 'center',
          dataIndex: 'SignDate',
          customRender: (text) => moment(text).format('YYYY-MM-DD'),
        },
        {
          title: '经办人',
          align: 'center',
          dataIndex: 'Operator',
          customRender: (text) => text || '/',
        },
        {
          title: '操作',
          align: 'center',
          width: '180px',
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getContractAccount(requestParameters).then((res) => res)
      },
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.searchLoading = true
      const getContractCategory = getDropdownList({
        PmKey: 'ContractCategory',
      })
      const getContractStatus = getDropdownList({
        PmKey: 'ContractStatus',
      })
      Promise.all([getContractCategory, getContractStatus])
        .then((res) => {
          this.searchList[0].props.options = res[0].map((e) => {
            return {
              value: e.DROPVALUE,
              label: e.DROPVALUE,
            }
          })
          this.searchList[1].props.options = res[1].map((e) => {
            return {
              value: e.DROPVALUE,
              label: e.DROPVALUE,
            }
          })
        })
        .finally(() => {
          this.searchLoading = false
        })
    },
    handleSearch(values) {
      const { PactClass, date } = values
      this.queryParam.PactClass = PactClass || ''
      if (!this.$isEmpty(date)) {
        values.StartDate = date[0]
        values.EndTime = date[1]
      }
      delete values.PactClass
      delete values.date
      this.queryParam.strContent = values
      this.tableRefresh(true)
    },
    handleReset() {
      this.queryParam.strContent = {}
      this.tableRefresh(true)
    },
    tableRefresh(refresh = false) {
      this.$refs.table.refresh(refresh)
    },
    handleDetail(record) {
      this.contract = record
      this.visibleDetail = true
    },
    handleEdit(record) {
      this.contract = record
      this.visibleSign = true
    },
  },
}
</script>
<style lang="less" scoped>
/deep/ .ant-table td {
  white-space: nowrap;
}
</style>
